package com.example.localpic;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private Button btCamera, btLocalImg;

    String[] permissions = {Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE};
    List<String> mPermissionList;       // 将之前用户没有给的权限，放到这个集合中
    int requestCode;  // 100: 相机权限; 101: 手机存储权限

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findView();

        setListener();
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.main_bt_camera:
                    requestCode = 100;
                    if (ActivityCompat.checkSelfPermission(MainActivity.this,
                            Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(MainActivity.this,
                                new String[] {Manifest.permission.CAMERA}, requestCode);
                    } else {
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        // 200: 相机
                        startActivityForResult(intent,200);
                    }
                    break;
                case R.id.main_bt_localImg:
                    requestCode = 101;
                    if (ActivityCompat.checkSelfPermission(MainActivity.this,
                            Manifest.permission.READ_EXTERNAL_STORAGE)
                            != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(MainActivity.this,
                                new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, requestCode);
                    } else {
                        Intent intent = new Intent(MainActivity.this, LocalImgActivity.class);
                        startActivity(intent);
                    }
                    break;
                default:
                    break;
            }
        }
    };

    /**
     * This interface is the contract for receiving the results for permission requests.
     * 只要调用的requestPermissions请求权限，必定会触发onRequestPermissionsResult返回结果
     * @param requestCode The request code passed in ActivityCompat.requestPermissions
     * @param permissions The requested permissions. Never null.
     * @param grantResults The grant results for the corresponding permissions which is either
     *                     PackageManager.PERMISSION_GRANTED or PackageManager.PERMISSION_DENIED. Never null.
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 100) {
            boolean flag = false;  // true: 有权限还没给; false: 权限都给了
            // 遍历结果数组
            for (int i = 0; i < grantResults.length; ++i) {
                int grantResult = grantResults[i];
                if (grantResult != PackageManager.PERMISSION_GRANTED) {
                    flag = true;
                }
            }

            if (!flag) {        // 权限都给了，可以跳转到下一个页面
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent,200);
            }
        } else if (requestCode == 101) {
            boolean flag = false;  // true: 有权限还没给; false: 权限都给了
            // 遍历结果数组
            for (int i = 0; i < grantResults.length; ++i) {
                int grantResult = grantResults[i];
                if (grantResult != PackageManager.PERMISSION_GRANTED) {
                    flag = true;
                }
            }

            if (!flag) {        // 权限都给了，可以跳转到下一个页面
                Intent intent = new Intent(MainActivity.this, LocalImgActivity.class);
                startActivity(intent);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && requestCode == 200) {
            Bundle bundle = data.getExtras();
            Bitmap bm = (Bitmap) bundle.get("data");
        }
    }

    private void setListener() {
        btCamera.setOnClickListener(listener);
        btLocalImg.setOnClickListener(listener);
    }

    private void findView() {
        btCamera = findViewById(R.id.main_bt_camera);
        btLocalImg = findViewById(R.id.main_bt_localImg);
    }
}